package com.eugene.shiftscalendar;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.util.Log;

import java.text.CollationElementIterator;
import java.util.Calendar;
import java.util.Random;

/**
 * Created by Eugene on 08.04.14.
 */
public class NotepadPainter extends BasePainter{

    public NotepadPainter(WidgetSizes sizes)
    {
        super(sizes);
    }

    int mainLayout()
    {
        return R.layout.notepad;
    }

    protected Point getSize(WidgetSizes.WidgetType type)
    {
        return _sizes.getSize(type, 6);
    }


    protected void paintTitle(Canvas canvas, Calendar cal)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.Title);
        int szX = sizes.x;
        int szY = sizes.y;

        // background
        int Radius = szY * 10 / 100;
        Paint grad = new Paint();
        grad.setAntiAlias(true);

        grad.setShader(new LinearGradient(0, 0, 0, szY, Color.argb(255,79,184,231), Color.argb(255,22,112,162), Shader.TileMode.MIRROR));
        Path path = new Path();
        path.moveTo(0,szY);
        path.lineTo(szX,szY);
        path.lineTo(szX,0);
        path.lineTo(Radius,0);
        path.arcTo(new RectF(0,0,2*Radius,2*Radius),-90,-90);
        path.lineTo(0,szY);

        canvas.drawPath(path, grad);

        grad.setShader(new LinearGradient(0, 0, 0, szY, Color.argb(255,39,144,191), Color.argb(255,32,122,172), Shader.TileMode.MIRROR));
        path.reset();
        path.moveTo(1,szY-1);
        path.lineTo(szX,szY-1);
        path.lineTo(szX,1);
        path.lineTo(Radius+1,1);
        path.arcTo(new RectF(1,1,2*Radius+1,2*Radius+1),-90,-90);
        path.lineTo(1,szY-1);

        canvas.drawPath(path, grad);

        // text
        Typeface flow = Typeface.createFromAsset(_context.getAssets(), "fonts/georgia.ttf");
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setSubpixelText(true);
        textPaint.setColor(Color.argb(255, 255, 255, 255));
        textPaint.setTypeface(flow);
        textPaint.setTextSize(szY/2);
        Rect bounds = new Rect();
        textPaint.getTextBounds("M",0,1,bounds);
        String[] months = {"Январь","Февраль","Март","Апрель","Май","Июнь","Июль","Август","Сентябрь","Октябрь","Ноябрь","Декабрь"};
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        canvas.drawText(months[month]+" "+year, Radius*3/2, szY/2 + bounds.height()/2, textPaint);
    }

    protected void paintPrev(Canvas canvas)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.Prev);
        int szX = sizes.x;
        int szY = sizes.y;

        // background
        Paint grad = new Paint();
        grad.setAntiAlias(true);

        grad.setShader(new LinearGradient(0, 0, 0, szY, Color.argb(255,79,184,231), Color.argb(255,22,112,162), Shader.TileMode.MIRROR));
        canvas.drawRect(0, 0, szX, szY, grad);

        grad.setShader(new LinearGradient(0, 0, 0, szY, Color.argb(255,39,144,191), Color.argb(255,32,122,172), Shader.TileMode.MIRROR));
        canvas.drawRect(0, 1, szX, szY - 1, grad);

        // Image
        int heightPercent = 50;
        Bitmap arrow = BitmapFactory.decodeResource(_context.getResources(),R.drawable.prev_month_1);
        int heightNew = szY*heightPercent/100;
        double scale = (double)arrow.getHeight() / heightNew;
        int widthNew = (int)(arrow.getWidth()/scale);
        canvas.drawBitmap(arrow,new Rect(0,0,arrow.getWidth(),arrow.getHeight()),
                new Rect(szX/2-widthNew/2,(50-heightPercent/2)*szY/100,szX/2+widthNew/2,(50+heightPercent/2)*szY/100),
                new Paint(Paint.FILTER_BITMAP_FLAG + Paint.ANTI_ALIAS_FLAG + Paint.DITHER_FLAG));

        //Log.d("Image info","IMAGE: w="+arrow.getWidth()+" h="+arrow.getHeight()+" scale="+scale+" wN="+widthNew+" hN="+heightNew);
    }

    protected void paintNext(Canvas canvas)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.Next);
        int szX = sizes.x;
        int szY = sizes.y;

        // background
        Paint grad = new Paint();
        grad.setAntiAlias(true);

        grad.setShader(new LinearGradient(0, 0, 0, szY, Color.argb(255,79,184,231), Color.argb(255,22,112,162), Shader.TileMode.MIRROR));
        canvas.drawRect(0,0,szX,szY,grad);

        grad.setShader(new LinearGradient(0, 0, 0, szY, Color.argb(255,39,144,191), Color.argb(255,32,122,172), Shader.TileMode.MIRROR));
        canvas.drawRect(0,1,szX,szY-1,grad);

        // Image
        int heightPercent = 50;
        Bitmap arrow = BitmapFactory.decodeResource(_context.getResources(),R.drawable.next_month_1);
        int heightNew = szY*heightPercent/100;
        double scale = (double)arrow.getHeight() / heightNew;
        int widthNew = (int)(arrow.getWidth()/scale);
        canvas.drawBitmap(arrow,new Rect(0,0,arrow.getWidth(),arrow.getHeight()),
                new Rect(szX/2-widthNew/2,(50-heightPercent/2)*szY/100,szX/2+widthNew/2,(50+heightPercent/2)*szY/100),
                new Paint(Paint.FILTER_BITMAP_FLAG + Paint.ANTI_ALIAS_FLAG + Paint.DITHER_FLAG));
    }

    protected void paintSettings(Canvas canvas)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.Settings);
        int szX = sizes.x;
        int szY = sizes.y;

        // background
        int Radius = szY * 10 / 100;
        Paint grad = new Paint();
        grad.setAntiAlias(true);

        grad.setShader(new LinearGradient(0, 0, 0, szY, Color.argb(255,79,184,231), Color.argb(255,22,112,162), Shader.TileMode.MIRROR));
        Path path = new Path();
        path.moveTo(0,szY);
        path.lineTo(szX,szY);
        path.lineTo(szX,Radius);
        path.arcTo(new RectF(szX-2*Radius,0,szX,2*Radius),0,-90);
        path.lineTo(0,0);
        path.lineTo(0, szY);

        canvas.drawPath(path, grad);

        grad.setShader(new LinearGradient(0, 0, 0, szY, Color.argb(255,39,144,191), Color.argb(255,32,122,172), Shader.TileMode.MIRROR));
        path.reset();
        path.moveTo(0, szY - 1);
        path.lineTo(szX - 1, szY - 1);
        path.lineTo(szX-1,Radius+1);
        path.arcTo(new RectF(szX - 2 * Radius - 1, 1, szX - 1, 2 * Radius + 1), 0, -90);
        path.lineTo(0, 1);
        path.lineTo(0,szY-1);

        canvas.drawPath(path, grad);

        // Image
        int heightPercent = 50;
        Bitmap arrow = BitmapFactory.decodeResource(_context.getResources(),R.drawable.settings_1);
        int heightNew = szY*heightPercent/100;
        double scale = (double)arrow.getHeight() / heightNew;
        int widthNew = (int)(arrow.getWidth()/scale);
        canvas.drawBitmap(arrow,new Rect(0,0,arrow.getWidth(),arrow.getHeight()),
                new Rect(szX/2-widthNew/2,(50-heightPercent/2)*szY/100,szX/2+widthNew/2,(50+heightPercent/2)*szY/100),
                new Paint(Paint.FILTER_BITMAP_FLAG + Paint.ANTI_ALIAS_FLAG + Paint.DITHER_FLAG));
    }

    protected void paintWeeks(Canvas canvas)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.Weeks);
        Point szLeft = getSize(WidgetSizes.WidgetType.LeftSide);
        Point szRight = getSize(WidgetSizes.WidgetType.RightSide);
        int szX = sizes.x;
        int szY = sizes.y;
        //init
        int YOffset = 5;
        int LeftOffset = getSize(WidgetSizes.WidgetType.LeftSide).x;
        int RightOffset = getSize(WidgetSizes.WidgetType.RightSide).x;
        int XSize = szX - LeftOffset - RightOffset;
        String days[] = {"ПН", "ВТ", "СР", "ЧТ", "ПТ", "СБ", "ВС"};

        // background
        Paint grad = new Paint();
        grad.setAntiAlias(true);
        grad.setColor(Color.argb(255,255,255,255));
        canvas.drawRect(szLeft.x/2,0,szX-szRight.x/2,szY,grad);
        grad.setShader(new LinearGradient(0, 0, 0, szY, Color.argb(120,0,0,0), Color.argb(0,0,0,0), Shader.TileMode.MIRROR));
        canvas.drawRect(szLeft.x/2,0,szX-szRight.x/2,szY/5,grad);

        Paint line = new Paint();
        line.setAntiAlias(true);
        line.setColor(Color.argb(255,180,180,180));
        canvas.drawLine(szLeft.x/2,szY,szX-szRight.x/2,szY,line);
        canvas.drawLine(szLeft.x/2,0,szLeft.x/2,szY,line);
        canvas.drawLine(szX-szRight.x/2,0,szX-szRight.x/2,szY,line);

        // text
        Typeface flow = Typeface.createFromAsset(_context.getAssets(), "fonts/georgia.ttf");
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setSubpixelText(true);
        textPaint.setColor(Color.argb(255,160,160,160));
        textPaint.setTextSize(szY/2);
        textPaint.setTypeface(flow);
        for(int i=0;i<7;i++){
            canvas.drawText(days[i], LeftOffset + XSize/14 + i*XSize/7 - textPaint.measureText(days[i])/2, szY-YOffset, textPaint);
        }
    }

    protected void paintDay(Canvas canvas, Calendar cal)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.Day);
        int szX = sizes.x;
        int szY = sizes.y;

        // background
        canvas.drawColor(Color.argb(255,255,255,255));
        Paint line = new Paint();
        line.setAntiAlias(true);
        line.setColor(Color.argb(255,200,200,200));
        if(dayCol!=6) canvas.drawLine(szX,szY,szX,0,line); // right
        if(dayRow!=0) canvas.drawLine(szX,0,0,0,line); // up
        if(dayCol!=0) canvas.drawLine(0,0,0,szY,line); // left
        if(dayRow!=5) canvas.drawLine(0,szY,szX,szY,line); // bottom

        // today
        Calendar todayCal = Calendar.getInstance();
        boolean today = todayCal.get(Calendar.YEAR) == cal.get(Calendar.YEAR) && todayCal.get(Calendar.MONTH) == cal.get(Calendar.MONTH) &&
                todayCal.get(Calendar.DAY_OF_MONTH) == cal.get(Calendar.DAY_OF_MONTH);

        // text
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(_context);
        int month = sp.getInt("CurrMonth",cal.get(Calendar.MONTH));

        if(today && cal.get(Calendar.MONTH) == month){
            Bitmap marker = BitmapFactory.decodeResource(_context.getResources(),R.drawable.notepad_back);
            int widthNew = szX;
            double scale = (double)marker.getWidth() / widthNew * 0.8;
            int heightNew = (int)(marker.getHeight()/scale);
            canvas.drawBitmap(marker,new Rect(0,0,marker.getWidth(),marker.getHeight()),
                    new Rect(szX/2-widthNew/2,szY/2-heightNew/2,szX/2+widthNew/2,szY/2+heightNew/2),
                    new Paint(Paint.FILTER_BITMAP_FLAG + Paint.ANTI_ALIAS_FLAG + Paint.DITHER_FLAG));
        }

        Typeface flow = Typeface.createFromAsset(_context.getAssets(), "fonts/notoserif-regular.ttf");
        Paint textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setSubpixelText(true);
        if(today && cal.get(Calendar.MONTH) == month){
            textPaint.setColor(_dateType.isHoliday(cal,_context) ? Color.argb(200, 170, 44, 44) : Color.argb(200, 20, 20, 20));
        }
        else{
            if(cal.get(Calendar.MONTH) != month){
                textPaint.setColor(_dateType.isHoliday(cal,_context) ? Color.argb(255, 200, 200, 200) : Color.argb(255, 200, 200, 200));
            }
            else{
                textPaint.setColor(_dateType.isHoliday(cal,_context) ? Color.argb(255, 180, 32, 32) : Color.argb(255, 12, 12, 12));
            }
        }
        textPaint.setTextSize(szY*2/5);
        textPaint.setTypeface(flow);
        Rect bounds = new Rect();
        String text = "" + cal.get(Calendar.DAY_OF_MONTH);
        textPaint.getTextBounds(text,0,text.length(), bounds);
        canvas.drawText(text, szX/2 - bounds.width()/2, szY/2 + bounds.height()/2, textPaint);

        if(_dateType.isUserChanged(cal, _context) && cal.get(Calendar.MONTH) == month){
            Paint grad = new Paint();
            grad.setAntiAlias(true);
            grad.setColor(Color.argb(255, 200, 200, 200));
            Rect corner = new Rect((int)(szX - szX*0.2 - 2), 2, szX-2, (int)(2 + szY*0.2));

            Path path = new Path();
            path.moveTo(corner.left, corner.top);
            path.lineTo(corner.right, corner.top);
            path.lineTo(corner.right, corner.bottom);
            path.lineTo(corner.left, corner.top);

            canvas.drawPath(path, grad);
        }
    }

    protected void paintFooter(Canvas canvas)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.Footer);
        Point szLeft = getSize(WidgetSizes.WidgetType.LeftSide);
        Point szRight = getSize(WidgetSizes.WidgetType.RightSide);
        int szX = sizes.x;
        int szY = sizes.y;
        int szW = szX - szLeft.x/2 - szRight.x/2;

        // background
        int Radius = szY/2;
        Paint grad = new Paint();
        grad.setAntiAlias(true);
        grad.setColor(Color.argb(255,180,180,180));
        Path path = new Path();
        path.moveTo(szLeft.x/2,0);
        path.arcTo(new RectF(szLeft.x/2,-Radius,szLeft.x/2+2*Radius,Radius),180,-90);
        path.lineTo(szX-szRight.x/2-Radius, Radius);
        path.arcTo(new RectF(szX-szRight.x/2-2*Radius,-Radius,szX-szRight.x/2,Radius),90,-90);
        path.lineTo(szLeft.x/2,0);
        canvas.drawPath(path,grad);

        grad.setColor(Color.argb(255,255,255,255));
        path.reset();
        path.moveTo(szLeft.x/2+1,0);
        path.arcTo(new RectF(szLeft.x/2+1,-Radius-1,2*Radius+szLeft.x/2+1,Radius-1),180,-90);
        path.lineTo(szX-szRight.x/2-Radius-1, Radius-1);
        path.arcTo(new RectF(szX-szRight.x/2-2*Radius-1,-Radius-1,szX-szRight.x/2-1,Radius-1),90,-90);
        path.lineTo(szLeft.x/2+1,0);
        canvas.drawPath(path,grad);

        grad.setColor(Color.argb(255,180,180,180));
        path.reset();
        path.moveTo(szLeft.x/2 + (float)0.5*Radius,Radius);
        path.lineTo(szLeft.x/2 + (float)1.5*Radius,(float)1.8*Radius);
        path.lineTo(szX - szRight.x/2 - (float)1.5*Radius,(float)1.8*Radius);
        path.lineTo(szX - szRight.x/2 - (float)0.5*Radius, Radius);
        path.lineTo(Radius,Radius);
        canvas.drawPath(path,grad);

        grad.setColor(Color.argb(255,220,220,220));
        path.reset();
        path.moveTo(szLeft.x/2 + (float)0.5*Radius+1,Radius+1);
        path.lineTo(szLeft.x/2 + (float)1.5*Radius+1,(float)1.8*Radius-1);
        path.lineTo(szX - szRight.x/2 - (float)1.5*Radius-1,(float)1.8*Radius);
        path.lineTo(szX - szRight.x/2 - (float)0.5*Radius-1, Radius+1);
        path.lineTo(Radius+1,Radius+1);
        canvas.drawPath(path,grad);
    }

    protected void paintLeftSide(Canvas cnv)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.LeftSide);
        int szX = sizes.x;
        int szY = sizes.y;

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.argb(255,255,255,255));

        cnv.drawRect(szX/2,0,szX,szY,paint);

        paint.setColor(Color.argb(255,180,180,180));
        cnv.drawLine(szX/2, 0, szX/2, szY, paint);
    }

    protected void paintRightSide(Canvas cnv)
    {
        Point sizes = getSize(WidgetSizes.WidgetType.RightSide);
        int szX = sizes.x;
        int szY = sizes.y;

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.argb(255,255,255,255));

        cnv.drawRect(0,0,szX/2+1,szY,paint);

        paint.setColor(Color.argb(255,180,180,180));
        cnv.drawLine(szX/2+1, 0, szX/2+1, szY, paint);
    }

    protected void paintUpper(Canvas cnv){}
}
